package com.personal.common.utils;

import org.apache.commons.lang.StringUtils;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;

/** 
 * @Description 孟加拉表计算根密钥
 * @author  jun
 * @Copyright 2012 hexing Inc. All rights reserved
 * @time：2012-8-11
 * @version AMI3.0 
 */
public class AESUtils {
	/**
	 * AES 加密 （仅仅AES/ECB/128）
	 * @param sourceData 需要加密的字符串
	 * @param encryptKey 加密密钥
	 * @return 加密后的字符串
	 */
	public static String encrypt(String sourceData, String encryptKey) {

		if (StringUtils.isBlank(sourceData)) {
			throw new NullPointerException("sourceData is null");
		}
		if (StringUtils.isBlank(encryptKey)) {
			throw new NullPointerException("encryptKey is null");
		}

		byte[] K = Hex.decode(encryptKey.getBytes());
		byte[] P = Hex.decode(sourceData.getBytes());
		
		CipherParameters parameters = new KeyParameter(K);

		BlockCipher encCipher = new AESFastEngine();

		encCipher.init(true, parameters);
		encCipher.processBlock(P, 0, P, 0);
		return new String(Hex.encode(P));
	}
}
